package com.example.demo.utils.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.Arrays;
import java.util.Properties;

//后期对查询和更新做拦截封装
//@Intercepts({@Signature(
//        type = Executor.class,
//        method = "update",
//        args = {MappedStatement.class, Object.class}
//), @Signature(
//        type = Executor.class,
//        method = "query",
//        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
//)})
public class UserUidInterceptor implements Interceptor {

    private static final String DB_TYPE = "postgresql";
    private static final String T_USER_INFO = "t_user_info";
    private static final String WHERE = "where";


    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        this.processRouting(invocation);
        return invocation.proceed();
    }

    private void processRouting(Invocation invocation){
        Object[] objects = invocation.getArgs();
        System.out.println("-----objects-------: "+ Arrays.toString(objects));
        MappedStatement mappedStatement = (MappedStatement) objects[0];
        System.out.println("-----mappedStatement-------: "+ mappedStatement);
        BoundSql boundSql = mappedStatement.getSqlSource().getBoundSql(objects[1]);
        System.out.println("-----boundSql-------: "+ boundSql);
        String pgsql = boundSql.getSql();
        if(!pgsql.contains(T_USER_INFO)){
            return;
        }

        Object parameter = boundSql.getParameterObject();
        if(parameter==null){
            return;
        }

        Long uid = null;
        String foundColumnName;
        if(mappedStatement.getSqlCommandType() == SqlCommandType.INSERT){

        }


    }

    @Override
    public Object plugin(Object o) {
        return null;
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
